﻿#include "devicestatus.h"
#include <QDebug>
#include "../commons/configfile.h"
#include "../commons/configpin.h"
#include "../logs/printlogs.h"
#include "./commons/pinsinfors.h"
#include "./deviceconversion.h"


DeviceStatus * DeviceStatus::m_deviceStatus = NULL;
DeviceStatus::DeviceStatus(QObject *parent) : QObject(parent)
  , m_serial(NULL)
  , m_isCleans(NULL)
  , m_isCleanPause(NULL)
  , m_isOut(NULL)

{
    m_comAlgor = new ComAlgor();
    m_serial = new QSerialPort();
    connect(m_serial,SIGNAL( readyRead() ),this,SLOT( onDataReceived() ));

    initComSeriel();
}


DeviceStatus::~DeviceStatus()
{

}

DeviceStatus *DeviceStatus::gestance()
{
    if(m_deviceStatus == NULL){
        m_deviceStatus = new DeviceStatus();
    }

    return m_deviceStatus;
}

void DeviceStatus::initComSeriel()
{
    QString names =  ConfigFile::gestance()->getCom3Name();
#ifdef DEBUG_INFOR
    qDebug()<<"names=="<<names;
#endif
    if(names.length() > 0){
#ifdef DEBUG_INFOR
        qDebug()<<__func__<<__LINE__;
#endif
        QSerialPortInfo info(names);
        m_serial->setPortName(names);
         qDebug()<<__func__<<__LINE__<<"PortName=="<<names;
        m_serial->setBaudRate(QSerialPort::Baud115200);
        m_serial->setParity(QSerialPort::NoParity);
        m_serial->setDataBits(QSerialPort::Data8);
        m_serial->setStopBits(QSerialPort::OneStop);
        m_serial->setFlowControl(QSerialPort::NoFlowControl);

        if(m_serial->open(QIODevice::ReadWrite)) // 以读写方式打开串口
        {
#ifdef DEBUG_INFOR
            qDebug()<<__func__<<__LINE__;
#endif
        }
#ifdef DEBUG_INFOR
        qDebug()<<__func__<<__LINE__;
#endif
    }
}

void DeviceStatus::setIsCleans(bool *isCleans)
{
    m_isCleans = isCleans;
}

void DeviceStatus::setIsCleanPause(bool *isCleanPause)
{
    m_isCleanPause = isCleanPause;
}

void DeviceStatus::setIsOut(bool *isOut)
{
    m_isOut = isOut;
}

void DeviceStatus::onDataReceived()
{
    if(m_serial != NULL){

        uint8_t cha[2];
        cha[0]='W';
        cha[1]='X';

        QByteArray data = m_serial->readAll();
        m_currentData.append(data);
        // #ifdef DEBUG_INFOR   qDebug()<<__func__<<__LINE__<<"data=="<<data.toHex();

        bool isEnd = false;
        uint8_t g_temp_check = 0;
        int uart3_lens = 0;
        QMap<int ,int> pinInfors;
        for (int i = 0; i < m_currentData.size(); i++) {
            if (m_currentData[i] == 'W' && m_currentData[i + 1] == 'X' && (i + 60 * 2 + 32 * 2 * 2 + 3 * 2) <=  m_currentData.size() ) {
                uart3_lens = (uint8_t) m_currentData[i + 7];
                uart3_lens = (uart3_lens & (int) 0x00FF);
                //   #ifdef DEBUG_INFOR   qDebug()<<__func__<<__LINE__<<"uart3_lens=="<<uart3_lens;

                if ((uart3_lens + i + 8) <= m_currentData.size()) {
                    g_temp_check = 0;

                    for (int j = i; j <= (uart3_lens + i + 7); j++) {

                        g_temp_check +=  (uint8_t) m_currentData[j];
                    }
                    //   #ifdef DEBUG_INFOR   qDebug()<<__func__<<__LINE__<<"g_temp_check=="<<g_temp_check;

                    if ( (uint8_t)m_currentData[uart3_lens + i + 8] == g_temp_check) {
#ifdef DEBUG_LOG_INFOR
                        QString printlogStr=typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)+" == "+m_currentData.toHex()  ;
                        PrintLogb::gestance()->printInfor(printlogStr);
#endif
                        uint8_t addr =  (uint8_t) m_currentData[i + 2];
                        uint8_t ver =  (uint8_t) m_currentData[i + 3];
                        short frame_id = 0;
                        frame_id = (uint8_t)(m_currentData[i + 4] );
                        frame_id = (frame_id << 8);
                        frame_id +=  (uint8_t)(m_currentData[i + 5] );
                        //    #ifdef DEBUG_INFOR   qDebug()<<__func__<<__LINE__<<"frame_id=="<<frame_id;

                        uint8_t uart3_type =  (uint8_t)m_currentData[i + 6];
                        //   #ifdef DEBUG_INFOR   qDebug()<<__func__<<__LINE__<<"uart3_type=="<<uart3_type;

                        for (int m = 0; m < 60; m++) {
                            uint8_t itemH =  (uint8_t)m_currentData[i + 9 + m * 2];

                            int numH = (itemH & 0x0FF);
                            //    #ifdef DEBUG_INFOR   qDebug()<<__func__<<__LINE__<<"m=="<<m;

                            //    #ifdef DEBUG_INFOR   qDebug()<<__func__<<__LINE__<<"numH=="<<numH;

                            mBuffStatus.insert(m,numH);

                        }

                        //     #ifdef DEBUG_INFOR   qDebug()<<__func__<<__LINE__<<"mBuffHeight=="<<mBuffHeight;
                        //     #ifdef DEBUG_INFOR   qDebug()<<__func__<<__LINE__<<"pinInfors=="<<pinInfors;


                        for (int j = 0; j < 32; j++) {
                            uint8_t itemH =  (uint8_t)m_currentData[i + 8 + 60 * 2 + 32 * 2 + j * 2];
                            uint8_t itemL =  (uint8_t)m_currentData[i + 9 + 60 * 2 + 32 * 2 + j * 2];
                            uint numH = (itemH & 0x0FF);
                            uint numL = (itemL & 0x0FF);
                            int numAll = (numH << 8) + numL;
                            //  #ifdef DEBUG_INFOR   qDebug()<<__func__<<__LINE__<<"numAll=="<<numAll;

                            mBuffCricle[j] = numAll;
                        }
#ifdef DEBUG_INFOR
                        qDebug()<<__func__<<__LINE__<<"mBuffCricle=="<<mBuffCricle;
#endif
                        for (int j = 0; j < 3; j++) {
                            uint8_t itemH =  (uint8_t)m_currentData[i + 8 + 60 * 2 + 32 * 2 + 32 * 2 + j * 2];
                            uint8_t itemL =  (uint8_t)m_currentData[i + 9 + 60 * 2 + 32 * 2 + 32 * 2 + j * 2];

                            uint numH = (itemH & 0x0FF);
                            uint numL = (itemL & 0x0FF);
                            int numAll = (numH << 8) + numL;
                            //    #ifdef DEBUG_INFOR   qDebug()<<__func__<<__LINE__<<"numAll=="<<numAll;

                            mBuffrempute[j] = numAll;

                        }

#ifdef DEBUG_INFOR
                        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"m_currentData=="<<m_currentData.toHex();
#endif
                        /*
                        if(m_isCleans != NULL && *m_isCleans == true){
                            isEnd = true;

                            m_currentData.clear();
                            break;
                        }

                        if(  (uint8_t)mBuffStatus[33-1]== 1 ||  (uint8_t)mBuffStatus[34-1]== 1){
                            //     #ifdef DEBUG_INFOR   qDebug()<<__func__<<__LINE__<<"mBuffStatus=="<<mBuffStatus;

                            //  emit  sigPinsInfor(pinInfors);
                            isEnd = true;

                            m_currentData.clear();
                            break;
                        }
                        */
                        bool isClens =false;
                        if(m_isCleans != NULL && *m_isCleans == true){
                            if(m_isCleanPause != NULL ){
                                if(*m_isCleanPause == true){

                                }else{

                                    QList<int> pinLista =  PinsInfors::gestance()->keyPins();


                                    for(int m =0 ; m< pinLista.size() ;m++){
                                        int ma = pinLista.at(m);
                                        PinData *  pinData =PinsInfors::gestance()->getPin(ma);
                                        if(pinData == NULL){
                                            continue;
                                        }
                                        int pinb1 = ConfigPin::gestance()->getRealPin(ma);
                                        if(pinb1 < 0){
                                            continue;
                                        }
                                        int pinb2 = mBuffCricle.value(pinb1 -1,0);
                                        int minElectric =  pinData->m_minElectric.toInt();
                                        int maxElectric = pinData->m_maxElectric.toInt();

                                        //                                        if(pinb2 < minElectric){
                                        //                                            emit sigElectric(pinb1 , ma ,1);
                                        //                                            isClens= true;
                                        //                                        }
                                        if(pinb2 > maxElectric){
                                            emit sigElectric(pinb1 , ma ,2);
                                            isClens= true;
                                        }
                                    }

                                }

                            }

                            if(mBuffStatus.value(0,0) == 1){
                                QJsonObject itObjeca =  DeviceConversion::gestance()->currentCleanIndex();

                                if(itObjeca.contains("cmds")){
                                    int cmds = itObjeca.take("cmds").toInt();
                                    int timeds =10;
                                    if(itObjeca.contains("times")){
                                        timeds = itObjeca.take("times").toInt();
                                    }

                                    if(cmds == 2){
                                        quint64 currentTimesa = QDateTime::currentDateTime().toTime_t();
                                        quint64 currentTimesb =DeviceConversion::gestance()->currentCleanTime();

                                        if((currentTimesa - currentTimesb) >= timeds  ){
                                            emit sigWarterHandl();

                                            isClens = true;
                                        }
                                    }

                                }
                            }
                        }

                        if(isClens){
                            emit  sigElectricHandl();
                        }
                        /*
                        if(  (uint8_t)mBuffStatus[33-1]== 1 ||  (uint8_t)mBuffStatus[34-1]== 1){
                            //     #ifdef DEBUG_INFOR   qDebug()<<__func__<<__LINE__<<"mBuffStatus=="<<mBuffStatus;

                            for (int j = 0; j < 32; j++) {
                                uint8_t itemH =  (uint8_t)m_currentData[i + 8 + 60 * 2 + j * 2];
                                uint8_t itemL =  (uint8_t)m_currentData[i + 9 + 60 * 2 + j * 2];


                                uint numH = (itemH & 0x0FF);
                                uint numL = (itemL & 0x0FF);
                                uint numAll = (numH << 8) + numL;
                                //    #ifdef DEBUG_INFOR   qDebug()<<__func__<<__LINE__<<"numAll=="<<numAll;

#ifdef DEBUG_INFOR
                            qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"j=="<<j<<"numAll=="<<numAll;
#endif
                                int userPinsa = ConfigPin::gestance()->getUserPin(j+1);
                                if(userPinsa > 0){
                                    pinInfors.insert(userPinsa,numAll);
                                }

                            }
                            //   emit  sigPinsInfor(pinInfors);
                            isEnd = true;

                            m_currentData.clear();
                            break;

                        }*/
                        //  #ifdef DEBUG_INFOR   qDebug()<<__func__<<__LINE__<<"mBuffStatus=="<<mBuffStatus;

                        for (int j = 0; j < 32; j++) {
                            uint8_t itemH =  (uint8_t)m_currentData[i + 8 + 60 * 2 + j * 2];
                            uint8_t itemL =  (uint8_t)m_currentData[i + 9 + 60 * 2 + j * 2];


                            uint numH = (uint8_t)(itemH & 0x0FF);
                            uint numL = (uint8_t)(itemL & 0x0FF);
                            uint numAll = (numH << 8) + numL;
                            //    #ifdef DEBUG_INFOR   qDebug()<<__func__<<__LINE__<<"numAll=="<<numAll;
#ifdef DEBUG_INFOR
                            qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"j=="<<j<<"numAll=="<<numAll;
#endif
                            mBuffHeight[j] = numAll;
                            int userPinsa = ConfigPin::gestance()->getUserPin(j+1);
                            if(userPinsa > 0){
                                pinInfors.insert(userPinsa,numAll);
                            }

                        }

#ifdef DEBUG_INFOR
                        qDebug()<<__func__<<__LINE__<<"mBuffrempute=="<<mBuffrempute;
#endif
                        //                        if(  (uint8_t)mBuffStatus[33-1]!= 1 ||  (uint8_t)mBuffStatus[34-1]!= 1){
                        //                            //     #ifdef DEBUG_INFOR   qDebug()<<__func__<<__LINE__<<"mBuffStatus=="<<mBuffStatus;

                        //                            //  emit  sigPinsInfor(pinInfors);

                        //                        }
                        //  #ifdef DEBUG_INFOR   qDebug()<<__func__<<__LINE__<<"mBuffStatus=="<<mBuffStatus;

                        isEnd = true;

                        m_currentData.clear();
                    }

                }
            }
        }



    }

}


